package com.itlong.cloud.controller.parking;

import com.itlong.cloud.POJO.DTO.operate.OperatePlanManageInfoDTO;
import com.itlong.cloud.POJO.DTO.operate.OperatePlanManagePageDTO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.operate.OperatePlanManageInfoVO;
import com.itlong.cloud.annotation.AuthAccessRequired;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.OperateErrorCodeEnum;
import com.itlong.cloud.equip.operate.IOperatePlanManageServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

/**
 * <desc>
 *      运营-智慧停车-设备方案管理。
 * <desc/>
 *
 * @createDate 2018-11-05.
 */
@RestController
@RequestMapping("/parking/planManage")
public class OperatePlanManageController {

    private static final Logger LOG = LoggerFactory.getLogger(OperatePlanManageController.class);

    @Autowired
    IOperatePlanManageServiceClient iOperatePlanManageServiceClient;

    /**
     * <desc>
     *      设备方案信息分页查询
     * <desc/>
     *
     * @param operatePlanManagePageDTO 分页查询参数
     * @return
     * @author Qiang.S
     * @createDate 2018-11-05
     */
    @ResponseBody
    @AuthAccessRequired
    @RequestMapping(path = "/getPlanInfoPage", method = RequestMethod.POST)
    public Object getPlanInfoPage(OperatePlanManagePageDTO operatePlanManagePageDTO){
        MessageVO messageVO;
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            operatePlanManagePageDTO= HttpProtocolUtil.parseRequestParamToDTO(OperatePlanManagePageDTO.class,request);
            //请求对象不能为空
            if(null == operatePlanManagePageDTO){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),iOperatePlanManageServiceClient.getPlanInfoPage(operatePlanManagePageDTO));
        }catch (Exception e){
            throw new DataAccessException("【运营_智慧停车_设备方案信息分页查询getPlanInfoPage】设备方案信息分页查询失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      添加
     * <desc/>
     *
     * @param operatePlanManageInfoDTO 设备方案信息
     * @return
     * @author Qiang.S
     * @createDate 2018-11-05
     */
    @ResponseBody
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.PARKING_PLAN_MANAGE_SAVE_LOG_DESC)
    @RequestMapping(path = "/save", method = RequestMethod.POST)
    public Object save(OperatePlanManageInfoDTO operatePlanManageInfoDTO){
        MessageVO messageVO;
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            operatePlanManageInfoDTO= HttpProtocolUtil.parseRequestParamToDTO(OperatePlanManageInfoDTO.class,request);
            //请求对象不能为空
            if(null == operatePlanManageInfoDTO){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            //方案名称不能为空
            if(StringUtils.isBlank(operatePlanManageInfoDTO.getPlanName())){
                return new MessageVO(OperateErrorCodeEnum.PLAN_MANAGE_PLANNAME_EMPTY_ERR.getErrorCode());
            }
            //方案名称不能为重复
            if(iOperatePlanManageServiceClient.checkPlanName(operatePlanManageInfoDTO.getPlanName(),operatePlanManageInfoDTO.getPlanId()) > 0){
                return new MessageVO(OperateErrorCodeEnum.PLAN_MANAGE_PLANNAME_REPEAT_ERR.getErrorCode());
            }
            iOperatePlanManageServiceClient.save(operatePlanManageInfoDTO);
            messageVO = new MessageVO(operatePlanManageInfoDTO.getPlanName(),BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【运营_智慧停车添加save】添加失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      更新
     * <desc/>
     *
     * @param operatePlanManageInfoDTO 设备方案信息
     * @return
     * @author Qiang.S
     * @createDate 2018-11-05
     */
    @ResponseBody
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.PARKING_PLAN_MANAGE_UPDATE_LOG_DESC)
    @RequestMapping(path = "/update", method = RequestMethod.POST)
    public Object update(OperatePlanManageInfoDTO operatePlanManageInfoDTO){
        MessageVO messageVO;
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            operatePlanManageInfoDTO= HttpProtocolUtil.parseRequestParamToDTO(OperatePlanManageInfoDTO.class,request);
            //请求对象不能为空
            if(null == operatePlanManageInfoDTO){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            //方案编号不能为空
            if(StringUtils.isBlank(operatePlanManageInfoDTO.getPlanId())){
                return new MessageVO(OperateErrorCodeEnum.PLAN_MANAGE_PLANID_EMPTY_ERR.getErrorCode());
            }
            //方案名称不能为空
            if(StringUtils.isBlank(operatePlanManageInfoDTO.getPlanName())){
                return new MessageVO(OperateErrorCodeEnum.PLAN_MANAGE_PLANNAME_EMPTY_ERR.getErrorCode());
            }
            //方案名称不能为重复
            if(iOperatePlanManageServiceClient.checkPlanName(operatePlanManageInfoDTO.getPlanName(),operatePlanManageInfoDTO.getPlanId()) > 0){
                return new MessageVO(OperateErrorCodeEnum.PLAN_MANAGE_PLANNAME_REPEAT_ERR.getErrorCode());
            }
            iOperatePlanManageServiceClient.update(operatePlanManageInfoDTO);
            messageVO = new MessageVO(operatePlanManageInfoDTO.getPlanName(),BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【运营_智慧停车更新update】更新失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      删除
     * <desc/>
     *
     * @param operatePlanManageInfoDTO 设备方案信息
     * @return
     * @author Qiang.S
     * @createDate 2018-11-05
     */
    @ResponseBody
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.PARKING_PLAN_MANAGE_DELETE_LOG_DESC)
    @RequestMapping(path = "/delete", method = RequestMethod.POST)
    public Object delete(OperatePlanManageInfoDTO operatePlanManageInfoDTO){
        MessageVO messageVO;
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            operatePlanManageInfoDTO= HttpProtocolUtil.parseRequestParamToDTO(OperatePlanManageInfoDTO.class,request);
            //请求对象不能为空
            if(null == operatePlanManageInfoDTO){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            //方案编号不能为空
            if(StringUtils.isBlank(operatePlanManageInfoDTO.getPlanId())){
                return new MessageVO(OperateErrorCodeEnum.PLAN_MANAGE_PLANID_EMPTY_ERR.getErrorCode());
            }
            OperatePlanManageInfoVO operatePlanManageInfoVO =  iOperatePlanManageServiceClient.getDetail(operatePlanManageInfoDTO.getPlanId());
            //方案编号不能为空
            if(!"0".equals(operatePlanManageInfoVO.getParkingCount())){
                return new MessageVO(OperateErrorCodeEnum.PLAN_MANAGE_PLANID_IS_EXIST.getErrorCode());
            }
            iOperatePlanManageServiceClient.delete(operatePlanManageInfoDTO);
            messageVO = new MessageVO(operatePlanManageInfoDTO.getPlanName(),BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【运营_智慧停车删除delete】删除失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      根据方案编号获取名称
     * <desc/>
     *
     * @param planId 方案编号
     * @return
     * @author Qiang.S
     * @createDate 2018-11-05
     */
    @ResponseBody
    @RequestMapping(path = "/getDetail", method = RequestMethod.POST)
    public Object getDetail(String planId){
        MessageVO messageVO;
        try {
            HttpServletRequest request= ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            planId=request.getAttribute("planId")==null?null:request.getAttribute("planId").toString();
            //方案编号不能为空
            if(StringUtils.isBlank(planId)){
                return new MessageVO(OperateErrorCodeEnum.PLAN_MANAGE_PLANID_EMPTY_ERR.getErrorCode());
            }
            messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),iOperatePlanManageServiceClient.getDetail(planId));
        }catch (Exception e){
            throw new DataAccessException("【运营_智慧停车_根据方案编号获取名称getDetail】根据方案编号获取名称失败",e);
        }
        return messageVO;
    }

}
